/*
 * @Author: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Date: 2025-10-10 09:38:39
 * @LastEditTime: 2025-10-10 16:19:11
 * @LastEditors: fuhongliang-TSJ hongliang.fu@4d-bios.com
 * @Description: 
 * @FilePath: \lc_-esp32_-s3\main\lc\handheld\att_app.c
 * 立创实战派ESP32-S3开发板
 * Copyright (c) 2025 by 4D Bios, All Rights Reserved. 
 */

#include <math.h>
#include "handheld.h"
#include "bsp_lvgl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "qmi8658.h"

extern lv_obj_t *icon_in_obj; // 应用界面
extern int icon_flag;

extern i2c_obj_t i2c0_master;

static lv_obj_t * label_x; // x角度值
static lv_obj_t * label_y; // y角度值
static lv_obj_t * label_z; // z角度值
static lv_obj_t * x_bar;   // x角度bar
static lv_obj_t * y_bar;   // y角度bar
static lv_obj_t * z_bar;   // z角度bar

static lv_obj_t * label_title; // 标题栏文字

static lv_timer_t * my_lv_timer;

// 声明使用的字体
LV_FONT_DECLARE(font_alipuhui20);

/**
 * @description: 返回主界面按钮事件处理函数
 * @param {lv_event_t} *e
 * @return {*}
 */
static void btn_back_cb(lv_event_t *e)
{
    lv_obj_del(icon_in_obj); //删除画布
    lv_timer_del(my_lv_timer);
    qmi8658_close(); // 关闭芯片运行
    icon_flag = 0;
}

/**
 * @description:  定时更新姿态角度值
 * @param {lv_timer_t *} timer
 * @return {*}
 */
static void att_update_cb(lv_timer_t * timer)
{
    qmi8658_t qmi8658;
    int att_x, att_y, att_z;
    // 获取XYZ角度
    qmi8658_fetch_angleFromAcc(&qmi8658);
    att_x = round(qmi8658.AngleX);
    att_y = round(qmi8658.AngleY);
    att_z = round(qmi8658.AngleZ);

    // 更新角度值
    lv_label_set_text_fmt(label_x, "X: %d", att_x);
    lv_label_set_text_fmt(label_y, "Y: %d", att_y);
    lv_label_set_text_fmt(label_z, "Z: %d", att_z);

    // 更新角度bar
    lv_bar_set_start_value(x_bar, att_x-10, LV_ANIM_OFF);
    lv_bar_set_value(x_bar, att_x+10, LV_ANIM_OFF);
    lv_bar_set_start_value(y_bar, att_y-10, LV_ANIM_OFF);
    lv_bar_set_value(y_bar, att_y+10, LV_ANIM_OFF);
    lv_bar_set_start_value(z_bar, att_z-10, LV_ANIM_OFF);
    lv_bar_set_value(z_bar, att_z+10, LV_ANIM_OFF);

    // 判断运动状态
    uint8_t status = qmi8658_fetch_motion();
    if (status & 0x20) // 判断是否发生Any-Motion
    {
        lv_label_set_text(label_title, "运动或震动");
    }
    else if (status & 0x40) // 判断是否发生No-Motion
    {
        lv_label_set_text(label_title, "静止");
    }
    else if (status & 0x80) // 判断是否发生Significant-Motion
    {
        lv_label_set_text(label_title, "剧烈运动");
    }
}

/**
 * @description: 姿态任务
 * @param {void} *arg
 * @return {*}
 */
void task_process_att(void *arg)
{
    qmi8658_init(i2c0_master);

    lvgl_port_lock(0);

    // 显示x角度值
    label_x = lv_label_create(icon_in_obj);
    lv_label_set_text(label_x, "X:");
    lv_obj_set_style_text_font(label_x, &lv_font_montserrat_20, 0);
    lv_obj_align(label_x, LV_ALIGN_TOP_LEFT, 20, 60);

    // 显示x角度bar
    x_bar = lv_bar_create(icon_in_obj);
    lv_bar_set_mode(x_bar, LV_BAR_MODE_RANGE);
    lv_bar_set_range(x_bar, -101, 101);
    lv_bar_set_start_value(x_bar, -10, LV_ANIM_OFF);
    lv_bar_set_value(x_bar, 10, LV_ANIM_OFF);
    lv_obj_set_size(x_bar, 200, 25);
    lv_obj_align(x_bar, LV_ALIGN_TOP_LEFT, 80, 60);

    // 显示y角度值
    label_y = lv_label_create(icon_in_obj);
    lv_label_set_text(label_y, "Y:");
    lv_obj_set_style_text_font(label_y, &lv_font_montserrat_20, 0);
    lv_obj_align(label_y, LV_ALIGN_TOP_LEFT, 20, 120);

    // 显示y角度bar
    y_bar = lv_bar_create(icon_in_obj);
    lv_bar_set_mode(y_bar, LV_BAR_MODE_RANGE);
    lv_bar_set_range(y_bar, -101, 101);
    lv_bar_set_start_value(y_bar, -10, LV_ANIM_OFF);
    lv_bar_set_value(y_bar, 10, LV_ANIM_OFF);
    lv_obj_set_size(y_bar, 200, 25);
    lv_obj_align(y_bar, LV_ALIGN_TOP_LEFT, 80, 120);

    // 显示z角度值
    label_z = lv_label_create(icon_in_obj);
    lv_label_set_text(label_z, "Z:");
    lv_obj_set_style_text_font(label_z, &lv_font_montserrat_20, 0);
    lv_obj_align(label_z, LV_ALIGN_TOP_LEFT, 20, 180);

    // 显示z角度bar
    z_bar = lv_bar_create(icon_in_obj);
    lv_bar_set_mode(z_bar, LV_BAR_MODE_RANGE);
    lv_bar_set_range(z_bar, -101, 101);
    lv_bar_set_start_value(z_bar, -10, LV_ANIM_OFF);
    lv_bar_set_value(z_bar, 10, LV_ANIM_OFF);
    lv_obj_set_size(z_bar, 200, 25);
    lv_obj_align(z_bar, LV_ALIGN_TOP_LEFT, 80, 180);
    
    lvgl_port_unlock();

    // 创建一个lv_timer 用于更新角度
    my_lv_timer = lv_timer_create(att_update_cb, 200, NULL);

    vTaskDelete(NULL);
}

/**
 * @description: 姿态应用
 * @return {*}
 */
void att_app(void)
{
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, 10);
    lv_style_set_opa(&style, LV_OPA_COVER);
    lv_style_set_bg_color(&style, lv_color_hex(0xFFFFFF));
    lv_style_set_border_width(&style, 0);
    lv_style_set_pad_all(&style, 0);
    lv_style_set_width(&style, 320);
    lv_style_set_height(&style, 240);
    
    // 创建一个界面
    icon_in_obj = lv_obj_create(lv_scr_act());
    lv_obj_add_style(icon_in_obj, &style, 0);

    // 创建标题背景
    lv_obj_t *obj_title = lv_obj_create(icon_in_obj);
    lv_obj_set_size(obj_title, 320, 40);
    lv_obj_set_align(obj_title, LV_ALIGN_TOP_MID);
    lv_obj_set_style_pad_all(obj_title, 0, 0);  // 设置间隙
    lv_obj_set_style_bg_color(obj_title, lv_color_hex(0x30a830), 0);

    label_title = lv_label_create(obj_title);
    lv_label_set_text(label_title, "运动监测");
    lv_obj_set_style_text_font(label_title, &font_alipuhui20, 0);
    lv_obj_set_style_text_color(label_title, lv_color_hex(0xffffff), 0);
    lv_obj_set_align(label_title, LV_ALIGN_CENTER);

    // 返回按钮
    lv_obj_t *btn_back = lv_btn_create(obj_title);
    lv_obj_align(btn_back, LV_ALIGN_LEFT_MID, 0, 0);
    lv_obj_set_size(btn_back, 60, 30);
    lv_obj_set_style_border_width(btn_back, 0, 0); // 设置边框宽度
    lv_obj_set_style_pad_all(btn_back, 0, 0);  // 设置间隙
    lv_obj_set_style_bg_opa(btn_back, LV_OPA_0, 0);
    lv_obj_set_style_shadow_opa(btn_back, LV_OPA_TRANSP, LV_PART_MAIN); // 阴影透明
    lv_obj_add_event_cb(btn_back, btn_back_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *label_back = lv_label_create(btn_back);
    lv_label_set_text(label_back, LV_SYMBOL_LEFT);
    lv_obj_set_style_text_font(label_back, &lv_font_montserrat_20, 0);
    lv_obj_align(label_back, LV_ALIGN_CENTER, -10, 0);

    icon_flag = 1;

    xTaskCreatePinnedToCore(task_process_att, "task_process_att", 4 * 1024, NULL, 5, NULL, 1);
}